package com.flex.mobile.dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import javax.sql.DataSource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.simple.ParameterizedRowMapper;
import org.springframework.jdbc.core.simple.SimpleJdbcTemplate;
import org.springframework.stereotype.Service;

import com.flex.mobile.dao.model.DictionaryInfo;

@Service("dictionaryDao")
public class DictionaryDao {
	private final SimpleJdbcTemplate template;

	private final RowMapper<DictionaryInfo> summaryRowMapper = new ParameterizedRowMapper<DictionaryInfo>() {

		public DictionaryInfo mapRow(ResultSet rs, int rowNum) throws SQLException {
			DictionaryInfo model = new DictionaryInfo();
			model.setId(rs.getInt("id"));
			model.setType(rs.getString("TYPE"));
			model.setValue(rs.getInt("VALUE"));
			model.setEnable(rs.getInt("ENABLE"));
			return model;
		}
	};

	@Autowired
	public DictionaryDao(DataSource dataSource) {
		this.template = new SimpleJdbcTemplate(dataSource);
	}

	public List<DictionaryInfo> findAll() {
		return this.template.query("SELECT * FROM DICTIONARY_INFO", this.summaryRowMapper);
	}

	public List<DictionaryInfo> findByType(String type) {
		String sql = "select * from DICTIONARY_INFO where type = ";
		return this.template.query(sql, this.summaryRowMapper, type.trim());
	}

	public DictionaryInfo findById(long id) {
		
		String sql = "select * from DICTIONARY_INFO where id=?";
		return this.template.queryForObject(sql, this.summaryRowMapper, id);
	}

}
